package com.example.myapplication1;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CourseDetailActivity extends AppCompatActivity {
    private ImageView ivCourseCover;
    private TextView tvCourseTitle;
    private TextView tvCourseTeacher;
    private TextView tvCourseDifficulty;
    private RatingBar rbCourseScore;
    private TextView tvCourseDesc;

    private int courseId;
    private Call<Course> courseCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);

        // 获取课程ID
        courseId = getIntent().getIntExtra("courseId", 0);

        initView();
        initData();
    }

    private void initView() {
        ivCourseCover = findViewById(R.id.iv_course_detail_cover);
        tvCourseTitle = findViewById(R.id.tv_course_detail_title);
        tvCourseTeacher = findViewById(R.id.tv_course_detail_teacher);
        tvCourseDifficulty = findViewById(R.id.tv_course_detail_difficulty);
        rbCourseScore = findViewById(R.id.rb_course_detail_score);
        tvCourseDesc = findViewById(R.id.tv_course_detail_desc);
    }

    private void initData() {
        // 发起网络请求获取课程详情
        courseCall = RetrofitClient.getApiService().getCourseDetail(courseId);
        courseCall.enqueue(new Callback<Course>() {
            @Override
            public void onResponse(Call<Course> call, Response<Course> response) {
                if (response.isSuccessful() && response.body() != null) {
                    fillCourseData(response.body());
                } else {
                    Toast.makeText(CourseDetailActivity.this, "获取课程详情失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Course> call, Throwable t) {
                if (!call.isCanceled()) {
                    Toast.makeText(CourseDetailActivity.this, "网络错误，请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void fillCourseData(Course course) {
        Glide.with(this)
                .load(course.getCoverUrl())
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .into(ivCourseCover);

        tvCourseTitle.setText(course.getTitle());
        tvCourseTeacher.setText("讲师：" + course.getTeacher());
        tvCourseDifficulty.setText("难度：" + course.getDifficulty());
        rbCourseScore.setRating(course.getScore());
        tvCourseDesc.setText("课程详情：\n" + course.getDescription());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消网络请求，避免内存泄漏
        if (courseCall != null && !courseCall.isCanceled()) {
            courseCall.cancel();
        }
    }
}